package org.vm.cmbcdirect.wechat.listener;

import org.apache.log4j.Logger;
import org.vm.cmbcdirect.wechat.model.component.AccessToken;
import org.vm.cmbcdirect.wechat.service.CommonService;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.TimerTask;

/**
 * Created by mv on 2016/3/22.
 */
public class MainListener implements ServletContextListener{

    private final static Logger LOGGER = Logger.getLogger(MainListener.class);
    private static AccessToken accessToken = null;

    public void contextInitialized(ServletContextEvent servletContextEvent) {
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                accessToken = CommonService.getAccessToken();
                System.out.println("token:" + accessToken.getToken());
                LOGGER.info("token:" + accessToken.getToken());
            }
        };

//        Timer timer = new Timer();
//        long delay = 0;
//        long intevalPeroid = 15 * 60 * 1000;
//        timer.scheduleAtFixedRate(timerTask, delay, intevalPeroid);
    }

    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        // TODO
    }

    public static AccessToken getAccessToken() {
        return accessToken;
    }
}
